<?php
class Application_Model_PayBill extends Application_Model_DomainObject{
    
    //相关帐户
    protected $_accountId;
    
    //人民币金额
    protected $_money;
    
    //ID
    protected $_id;
    
    //支付状态，参考Application_Model_Status
    protected $_status;
    
    //支付类型，参考PayController里面的常量
    protected $_type;
    
    //创建日期
    protected $_dateCreated;
    
    //支付方式ID
    protected $_payId;
    
    //产生支付原因相关对象ID
    protected $_domainId;
    
    //支付描述
    protected $_description;
    
    //美元
    protected $_usmoney;
    
    //汇率
    protected $_rate;
    
    //汇兑损失率
    protected $_discountRate;
    
    //充值交易号
    protected $_txnId;


    public static function isExistTxnId($id){
        return Application_Model_PayBill::getDao()->queryOne(array('txnId'=>$id)) != NULL;
    }
    
    /**
     * 新创建
     */
    const PAY_STATUS_CREATED=0;

    /**
     * 支付成功
     */
    const PAY_STATUS_SUCCESS = 1;
    
    /**
     * 到了支付网站而没支付
     */
    const PAY_STATUS_GOPAY = 2;
    
    /**
     * 买家已经确定付款
     */
    const PAY_STATUS_FINISH = 3;
    
    /**
     * 买家已经确定付款
     */
    const PAY_STATUS_BACK = 4;
    //=========支付类型============
    
    /**
     * 充值 
     */
    const TYPE_RECHANGE=1;
    
    /**
     * 代购订单 
     */
    const TYPE_ORDER=2;
    
    /**
     * 国际运音 
     */
    const TYPE_WAYBILL=3;
    
    /**
     * 代付款 
     */
    const TYPE_AGENT_PAY=4;
    
    /**
     * 充值码 
     */
    const TYPE_ALI_CODE = 5;
    
    /**
     * 移动充值卡 
     */
    const TYPE_CARD = 6;
    
    /**
     * 代充值 
     */
    const TYPE_AGENT_RECHANGE =7;
    
    /**
     * 支付宝 
     */
    const PAY_ALIPAY =2;
    /**
     *Paypal支付 
     */
    const PAY_PAYPAL=1;
    /**
     * 使用帐户余额支付 
     */
    const PAY_ACCOUNT=3;
    
    /**
     * 使用国际信用卡
     */
    const PAY_CARD=4;


    public static function getStatusText($status){
        $map = array(NULL=>'未支付'
                ,Application_Model_PayBill::PAY_STATUS_CREATED=>'未支付'
                ,  Application_Model_PayBill::PAY_STATUS_FINISH=>'已完成'
                , Application_Model_PayBill::PAY_STATUS_GOPAY=>'未支付'
                , Application_Model_PayBill::PAY_STATUS_SUCCESS=>'支付成功，等待受理'
				, Application_Model_PayBill::PAY_STATUS_BACK=>'悠奇退款');
        return $map[$status];
    }

    /**
     * 返回类型名称
     * @param type $type
     * @return string 
     */
    public static function getTypeName($type){
        $map = array(Application_Model_PayBill::TYPE_AGENT_RECHANGE=>'充值'
                ,  Application_Model_PayBill::TYPE_ORDER=>'代购订单'
                ,  Application_Model_PayBill::TYPE_WAYBILL=>'国际运音'
                ,  Application_Model_PayBill::TYPE_AGENT_PAY=>'代支付'
                ,  Application_Model_PayBill::TYPE_ALI_CODE=>'支付宝充值码'
                ,  Application_Model_PayBill::TYPE_CARD=>'移动充值卡'
                ,  Application_Model_PayBill::TYPE_AGENT_RECHANGE=>'代充值');
        
        return $map[$type];
    }
    
    public static function getFilter($quick,$dateinbegin,$dateinend,$pagesize,$pageindex)
    {
        $where = array();
        $and = "";
        if($quick != ""){
            $uwhere[" nikename like "] = "%".$quick."%";
            $uwhere[" or storeno like "] = "%".$quick."%";
            $userItem = Application_Model_User::getDao()->queryAll($uwhere);
            if($userItem){
                $userid = "(";
                foreach ($userItem as $uservalue) {
                    $userid .= $uservalue->id.",";
                }
                $userid .= "0)";
                $where[" accountId in "] = $userid;
                $and = " and ";
            }else{
                return null;
            }
        }
        if($dateinbegin != ""){
            $where[$and." dateCreated>= "] = $dateinbegin;
            $and = " and ";
        }
        if($dateinend != ""){
            $where[$and." dateCreated<= "] = $dateinend;
            $and = " and ";
        }
        return Application_Model_PayBill::getDao()->queryPage($where, "dateCreated desc", $pagesize, $pageindex);
    }
    public function getuserinfo()
    {
        if($this->_accountId){
            return Application_Model_User::load($this->_accountId);
        }
    }
    public function getpayname()
    {
        if($this->_payId != ""){
            switch ($this->_payId) {
                case 1:
                    return "Paypal支付";
                    break;
                case 2:
                    return "支付宝支付";
                    break;
                case 3:
                    return "帐户余额支付";
                    break;
                case 4:
                    return "国际信用卡支付";
                    break;
                default:
                    break;
            }
        }
    }
     public function getstatusname()
    {
        if($this->_status != ""){
            switch ($this->_status)
            {
                case Application_Model_PayBill::PAY_STATUS_CREATED:
                  return "未支付";
                  break;
                case Application_Model_PayBill::PAY_STATUS_SUCCESS:
                  echo "已支付";
                  break;
                case Application_Model_PayBill::PAY_STATUS_GOPAY:
                  echo "未支付";
                  break;
                case Application_Model_PayBill::PAY_STATUS_FINISH:
                  echo "卖家已确认支付";
                  break;
                default:
                  echo "未知状态（$this->_status）";
            }   
        }
    }
    public function gettypenames()
    {
        if($this->_type != ""){
            switch ($this->_type) {
                case Application_Model_PayBill::TYPE_RECHANGE:
                    return "充值";
                    break;
                case Application_Model_PayBill::TYPE_ORDER:
                    return "代购订单";
                    break;
                case Application_Model_PayBill::TYPE_WAYBILL:
                    return "国际运单";
                    break;
                case Application_Model_PayBill::TYPE_AGENT_PAY:
                    return "代支付";
                    break;
                case Application_Model_PayBill::TYPE_ALI_CODE:
                    return "支付宝充值码";
                    break;
                case Application_Model_PayBill::TYPE_CARD:
                    return "移动充值卡";
                    break;
                case Application_Model_PayBill::TYPE_AGENT_RECHANGE:
                    return "代充值";
                    break;
                default:
                    return "未知类型($this->_type)";
                    break;
            }
        }
    }

}